package com.msf.tutorial.java8.chapter10;

import com.msf.tutorial.java8.Apple;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 流收集器
 */
public class C01Use {

    /**
     * 计数
     */
    public static void count() {
        List<String> list = Arrays.asList("Hello", "World", "Java", "Vue");
        // 计算集合的长度
        Long count = list.stream().collect(Collectors.counting());
        // 这种写法更直接
        count = list.stream().count();
    }

    /**
     * 最大值
     */
    public static void max() {
        List<Integer> list = Arrays.asList(2,1,3,4);
        list.stream().collect(Collectors.maxBy(Integer::compareTo)).ifPresent(System.out::println);
        // 简洁一点
        list.stream().max(Integer::compareTo).ifPresent(System.out::println);
    }

    /**
     * 最小值
     */
    public static void min() {
        List<Integer> list = Arrays.asList(2,1,3,4);
        list.stream().collect(Collectors.minBy(Integer::compareTo)).ifPresent(System.out::println);
        // 简洁一点
        list.stream().min(Integer::compareTo).ifPresent(System.out::println);
    }

    /**
     * 汇总
     */
    public static void sum() {
        List<Integer> list = Arrays.asList(2,1,3,4);
        list.stream().collect(Collectors.summingInt(i -> i));
    }

    /**
     * 平均值
     */
    public static void avg() {
        List<Integer> list = Arrays.asList(2,1,3,4);
        list.stream().collect(Collectors.averagingInt(i -> i));
    }

    /**
     * 多值同时获取
     * IntSummaryStatistics{count, sum, min, average, max}
     */
    public static void summarizing() {
        List<Integer> list = Arrays.asList(2,1,3,4);
        IntSummaryStatistics summarizing = list.stream().collect(Collectors.summarizingInt(i -> i));
        summarizing.getMax();
        summarizing.getMin();
        summarizing.getSum();
        summarizing.getAverage();
        summarizing.getCount();
    }

    /**
     * 连接字符串
     */
    public static void join() {
        List<String> list = Arrays.asList("Hello", "World");
        list.stream().collect(Collectors.joining(" "));
    }


    public static void main(String[] args) {
        max();
    }

}
